/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iteracon.generator.iterator;

import java.util.ListIterator;

/**
 *
 * @author martin
 */
public class FromToStepListIter implements ListIterator<Integer>{
    private int _from,
                _to,
                _step,
                _now;

    public FromToStepListIter(int from, int to, int step, int index){
      _from=from;
      _to=to;
      _step=step;
      _now = from+index*step;
    }
    
    @Override
    public boolean hasNext() {
      if (_step<0)
        return _now+_step >= _to;
      return _now+_step < _to;
    }

    @Override
    public Integer next() {
      int tmp = _now;
      _now = _now + _step;
      return tmp;
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException("Immutable");
    }

  @Override
  public boolean hasPrevious() {
      if (_step<0)
        return _now-_step >= _from;
      return _now-_step > _from;
  }

  @Override
  public Integer previous() {
      int tmp = _now;
      _now = _now - _step;
      return tmp;
  }

  @Override
  public int nextIndex() {
    return ((_now-_from)/_step)+1;
  }

  @Override
  public int previousIndex() {
    return ((_now-_from)/_step)-1;
  }

  @Override
  public void set(Integer e) {
      throw new UnsupportedOperationException("Immutable");
  }

  @Override
  public void add(Integer e) {
      throw new UnsupportedOperationException("Immutable");
  }
    
  }
